<template>
  <div class="btns">
    <Button v-auth="'probationEvaluate'" color="#6C7DA8" @click="comment">评价</Button>
    <Button v-auth="'probationPersuadeLeave'" color="error" @click="dieout">劝退</Button>
    <Button v-auth="'probationDimission'" color="#8B9FAB" @click="dimission">离职</Button>
    <Button v-auth="'probationRegular'" color="success" @click="probation">转正</Button>
  </div>
  <BasicModal :show="dieOutShow" title="劝退" width="30%" :widthComfirm="true" @comfirm="dieOutComfirm"
    @closeModal="dieOutShow = false">
    <template #content>
      <CommonForm :formList="dieOutFormList" ref="dieOutFormRef"></CommonForm>
    </template>
  </BasicModal>

  <BasicModal :show="commentShow" title="新增评价" width="30%" :widthComfirm="true" @comfirm="commentComfirm"
    @closeModal="commentShow = false">
    <template #content>
      <CommonForm :formList="commentFormList" ref="commentFormRef"></CommonForm>
    </template>
  </BasicModal>

  <BasicModal :show="probationShow" title="转正" width="30%" :widthComfirm="true" @comfirm="probationComfirm"
    @closeModal="probationShow = false">
    <template #content>
      <CommonForm :formList="formList" ref="formRef"></CommonForm>
    </template>
  </BasicModal>

  <BasicModal :show="dimissionShow" title="离职" width="30%" :widthComfirm="true" @comfirm="dimissionComfirm"
    @closeModal="dimissionShow = false">
    <template #content>
      <CommonForm :formList="dimissionFormList" ref="dimissionFormRef"></CommonForm>
    </template>
  </BasicModal>

</template>

<script setup lang='ts'>
import { Button } from "@/components/Button";
import { ref } from "vue"
import BasicModal from "@/views/ApplicationDetails/Modals/BasicModal.vue";
import type { IndexFormItem } from "@/views/ApplicationDetails/application-type.ts";
import CommonForm from "@/views/applicationDetails/CommonForm.vue";
import { message } from "ant-design-vue";
import {
  getEntrySchoolInfo, probationProgress, selectCommonConfigFoChildren, selectUserByRole
} from "@/api/application-details/index.ts";
import { comStores } from '@/store/common/comStore';
const store = comStores()
const probationShow = ref<boolean>(false);
const commentShow = ref<boolean>(false)
const dimissionShow = ref<boolean>(false);
const dieOutShow = ref<boolean>(false)
const { userName } = store.userInFor
const formList = ref<IndexFormItem>([
  { label: "转正", name: "cascader", value: "", component: 'cascader', options: [] },
  { label: "备注", name: "remark", value: "", component: 'textarea', required: false },
])

const commentFormList = ref<IndexFormItem[]>([
  { label: "评价", name: "tryoutAccess", value: "", component: 'textarea', required: true },
])

const dieOutFormList = ref<IndexFormItem[]>([
  { label: "劝退原因", name: "eliminateReason", value: "", component: 'textarea', require: false },
])


const dimissionFormList = ref<IndexFormItem[]>([
  { label: "离职原因", name: "leavingReason", value: "", component: 'textarea', },
])
const getOptions = async () => {
  const { resumeId } = store.currentResume
  const subject = await selectCommonConfigFoChildren({ type: 1 }) //科目
  formList.value[0].options = subject.data
  const info = await getEntrySchoolInfo({ resumeId, type: 2 });
  // const teacher = await selectUserByRole("MENTOR") //导师
  // formList.value[1].options = teacher.data

  if (info.data) {
    const { school, subject, grade, teacher } = info.data
    if(school){
     formList.value[0].value = [school, subject, grade] as any
    }
    // formList.value[1].value = teacher
  }
}



const emits = defineEmits(['surrender', 'dieOut'])
const comment = () => {
  commentShow.value = true
  console.log('comment 试用中')
}
const dimission = () => {
  dimissionShow.value = true
  console.log('quitPost 试用中')
}
const dieOutFormRef = ref<any>(null);
const dieout = () => {
  // emits('dieOut', '试用中')
  console.log('dieout 试用中')
  dieOutShow.value = true
}

const dieOutComfirm = async () => {
  const res = await dieOutFormRef.value.submitForm();
  if (!res) return;
  let param: any = {
    editStatus: "PUA"
  }
  res.forEach(el => {
    param[el.name] = el.value
  })

  const data = await send(param);
  if (!data) return
  dieOutFormList.value.forEach(el => el.value = "")
  dieOutShow.value = false
}

const surrender = () => {
  console.log('surrender 试用中')
  emits('surrender', '试用中')
}


const probation = () => {
  probationShow.value = true
  getOptions()
  console.log('probation', '试用中')
}
const formRef = ref<any>(null)
const probationComfirm = async () => {
  const res = await formRef.value.submitForm();
  if (!res) return;
  let param: any = {
    editStatus: "CONFIRMED"
  }
  res.forEach(el => {
    param[el.name] = el.value
  })
  param.tryoutSchool = param.cascader[0]
  param.tryoutSubject = param.cascader[1];
  param.tryoutGrade = param.cascader[2];
  const data = await send(param);
  if (!data) return
  formList.value.forEach(el => el.value = "")
  probationShow.value = false
}
const commentFormRef = ref<any>(null)
const commentComfirm = async () => {
  const res = await commentFormRef.value.submitForm();
  if (!res) return;
  let param = {
    editStatus: "ASSESS"
  }
  res.forEach(el => {
    param[el.name] = el.value
  })
  const data = await send(param);
  if (!data) return
  commentFormList.value.forEach(el => el.value = "")
  commentShow.value = false
}
const dimissionFormRef = ref<any>(null)
const dimissionComfirm = async () => {
  const res = await dimissionFormRef.value.submitForm();
  if (!res) return;
  let param = {
    editStatus: "DEPART"
  }
  res.forEach(el => {
    param[el.name] = el.value
  })
  const data = await send(param);
  if (!data) return
  dimissionFormList.value.forEach(el => el.value = "")
  dimissionShow.value = false
}

const send = async (param) => {
  const { resumeId, processId } = store.currentResume;
  param.resumeId = resumeId
  param.processId = processId
  param.operator = userName
  const data = await probationProgress(param);
  if (data && data.code === '10000') {
    store.getResumeInfo(resumeId)
    message.success("操作成功")
    return true
  } else {
    message.error(data.msg || "操作失败")
    return false
  }
}


</script>
<style lang="less" scoped>
.btns {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}
</style>
